# NEED TO BE FILLED OUT
import thread
import time



class Sensor(object):
	sensorCount = 0
	sensorThreads = []
	sensorList = []
	className = None
	
	
	def __init__(self,name="DEFAULT",minRaw=0,maxRaw=100,minEU=0,maxEU=100,unit="",readInterval=0.1,writeInterval=1):
		#attr_reader :name, :value, :minRaw, :maxRaw, :minEU, :maxEU, :unit, :simulate, :readInterval, :writeInterval			
		#attr_writer :minRaw, :maxRaw, :minEU, :maxEU, :unit, :simulate, :readInterval, :writeInterval
		
		self.readInterval = readInterval
		self.writeInterval = writeInterval
		self.simulate = False
		self.value = 0
		if name=="DEFAULT":
			name = "SENSOR_"
			name = name+str(Sensor.sensorCount+1)
		self.name 	= name
		self.minRaw = minRaw
		self.maxRaw = maxRaw
		self.minEU	= minEU
		self.maxEU	= maxEU
		self.unit	= unit
		
		createString  = ("NEW: ["+self.name+"]").ljust(25)
		createString += "["+str(self.minRaw).rjust(6)+" | "+str(self.maxRaw).rjust(6)+"]\t"
		createString += "["+str(self.minEU).rjust(6)+" | "+str(self.maxEU).rjust(6)+"]\t"
		createString += "["+str(self.unit).rjust(10)+"]"
		createString += "["+str(self.readInterval).rjust(3)+" | "+str(self.writeInterval).rjust(3)+"]"
		
		self.msg(createString)
		
		Sensor.sensorList.append(self)
		self.readerThread = thread.start_new_thread (self.readValue,(1,))
		Sensor.sensorThreads.append(self.readerThread)
		Sensor.sensorCount = Sensor.sensorCount+1
	
	def msg(self,message):
		print str(self.__class__.__name__).upper()+" "+message
	
	def Cmsg(cls,message):
		print str(cls.__name__).upper()+" "+message
	Cmsg = classmethod(Cmsg)
	
	def getName(self):
		print self.name
	
	def CgetList(cls):
		cls.Cmsg ("Current sensors:")
		for aSensor in Sensor.sensorList:
			cls.Cmsg ("   "+aSensor.name)
	CgetList = classmethod(CgetList)
	
	def readValue(self,param):
		while True:
			time.sleep(self.readInterval)
			# somehow get a value
			if self.simulate:
				self.value += 1
				if self.value>self.maxEU:
					self.value = self.minEU
			#Line below to check that simulator works
			#self.msg(self.name+": reading "+str(self.value))
			